﻿<template>
    <el-container>
        <el-header>
            <div class="left-panel">
                <el-button type="primary" icon="el-icon-plus" @click="opAdd">新增</el-button>
                <el-button type="info" plain icon="el-icon-delete" @click="opBatchDelete">删除</el-button>
            </div>
            <div class="right-panel">
                <el-button :icon="Search"   type="primary" @click="opBindData">查询</el-button>
                <el-button :icon="Search" type="info" plain @click="resetSearch">重置</el-button>
                <el-button text type="primary" @click="showCondition = !showCondition">
                    <span v-if="!showCondition">展开</span>
                    <span v-else>收起</span>
                    <vab-icon class="vab-dropdown" :class="{ 'vab-dropdown-active': fold }" icon="arrow-up-s-line" />
                </el-button>
            </div>
        </el-header>
        <el-foot v-show="showCondition">
            <vab-query-form-top-panel>
                <el-form inline label-width="49px" :model="queryForm" style="margin-top: 5px; margin-right: 20px">
                    <el-row type="flex" style="flex-wrap: wrap">
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
                            <el-form-item label="标题" label-width="100px" style="width: 100%">
                                <el-input placeholder="---请输入标题---" v-model="queryList.title" style="width: 100%" />
                            </el-form-item></el-col>
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
                            <el-form-item label="标签" label-width="100px" style="width: 100%">
                                <el-input placeholder="---请输入标签---" v-model="queryList.label" style="width: 100%" />
                            </el-form-item>
                        </el-col>
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
                            <el-form-item label-width="100px" label="类型" style="width: 100%">
                                <el-select placeholder="---请选择类型---" v-model="queryList.brandCode" style="width: 100%">
                                    <el-option v-for="item in optionsType" :key="item.value" :label="item.label"
                                        :value="item.value" />
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
                            <el-form-item label="状态" label-width="100px" style="width: 100%">
                                <el-select placeholder="---请选择状态---" v-model="queryList.status" style="width: 100%">
                                    <el-option label="---请选择---" value />
                                    <el-option v-for="item in optionsState" :key="item.value" :label="item.label"
                                        :value="item.value" />
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
                            <el-form-item label="创建时间" label-width="100px" style="width: 100%">
                                <el-date-picker style="width: 100%" v-model="queryList.crDates" type="datetimerange"
                                    value-format="YYYY-MM-DD HH:mm:ss" format="YYYY-MM-DD HH:mm:ss" range-separator="-"
                                    unlink-panels="true" start-placeholder="开始时间" end-placeholder="结束时间" :default-time="defaultTime"/>
                            </el-form-item>
                        </el-col>

                    </el-row>





                </el-form>
            </vab-query-form-top-panel>
        </el-foot>

        <el-main class="nopadding">
            <scTable ref="mytable" :queryType="'carousel-images/pages'" :data="listData" :queryList="queryList"
                :column="columnList" :orderList="OrderList" :sort-orders="['ascending', 'descending']"
                @selection-change="selectionChange" stripe show-summary remoteSummary>
                <el-table-column type="selection" width="50"></el-table-column>
                <el-table-column label="类型" prop="orgType" min-width="110" sortable>
                    <template #default="scope"> {{ scope.row.carouselImagesTypeDesc }}</template>
                </el-table-column>
                <el-table-column label="用户类型" prop="orgType" min-width="110" sortable>
                    <template #default="scope"> {{ scope.row.orgTypeDesc }}</template>
                </el-table-column>
                <el-table-column label="标题" prop="title" min-width="160" sortable show-overflow-tooltip>
                    <template #default="scope"> <span class="el-span-text"
                            @click="opShow(scope.row, scope.$index)">{{ scope.row.title }}</span></template>
                </el-table-column>
                <el-table-column label="标签" fixed="left" prop="label" min-width="125" sortable></el-table-column>
                <el-table-column label="查看次数" prop="watchQty" min-width="135" sortable></el-table-column>
                <el-table-column label="点赞次数" prop="likeQty" min-width="135" sortable></el-table-column>
                <el-table-column label="分享次数" prop="shareQty" min-width="135" sortable></el-table-column>
                <el-table-column label="备注" prop="remarks" min-width="140" sortable></el-table-column>
                <el-table-column label="创建时间" prop="createTime" min-width="100" sortable>
                    <template #default="scope">{{ scope.row.createTime ? scope.row.createTime?.slice(0, 10) : "" }}</template>
                </el-table-column>
                <el-table-column label="状态" fixed="right" align="center" min-width="120" prop="status" sortable>
                    <template #default="scope">
                        <el-switch v-model="scope.row.status" :active-value="1" :inactive-value="0"
                            @change="setStatus(scope.row)" />
                    </template>
                </el-table-column>
                <el-table-column label="操作" fixed="right" align="center" min-width="180">
                    <template #default="scope">
                        <el-button-group>
                            <el-button text type="primary" size="small"
                                @click="opShow(scope.row, scope.$index)">查看</el-button>
                            <el-button text type="primary" size="small"
                                @click="opEdit(scope.row, scope.$index)">编辑</el-button>
                            <el-popconfirm title="确定删除吗？" @confirm="opDelete(scope.row, scope.$index)">
                                <template #reference>
                                    <el-button text type="primary" size="small">删除</el-button>
                                </template>
                            </el-popconfirm>
                        </el-button-group>
                    </template>
                </el-table-column>
            </scTable>
        </el-main>
    </el-container>

    <save-dialog v-if="dialogSave" ref="saveDialog" @fatherMethod="opBindData" @success="handleSaveSuccess"
        @closed="dialogSave = false"></save-dialog>
</template>

<script>
import saveDialog from "./save";
import publicjs from "@/api/model/publicjs";
import tool from "@/utils/tool";
import config from "@/config";
export default {
    name: "carouselImages",
    components: {
        saveDialog,
        publicjs,
    },
    data() {
        return {
            dialogSave: false,
            showCondition: false,
            selectionRows: [],
            pkList: ["materialId"],
            columnList: [],
            OrderList: { materialId: "Asc" },
            queryList: {
                materialTypeIds: [],
            },
            result: "", // 运行结果
            props1: {
                checkStrictly: true,
                emitPath: false,
            },
            addState: true, //可以增加
            editState: true, //可以修改
            editBatchState: true, //可以批量修改
            deleteState: true, //可以删除
            optionsState: [],
            optionsType: [],
            isShow: false,//是否显示
            defaultTime:[
                new Date(2000, 1, 1, 0, 0, 0),
                new Date(2000, 2, 1, 23, 59, 59),
            ]
        };
    },
    methods: {
        //添加
        opAdd() {
            this.dialogSave = true;
            this.$nextTick(() => {
                this.$refs.saveDialog.open("Add");
            });
        },
        //编辑
        opEdit(row) {
            this.dialogSave = true;
            this.$nextTick(() => {
                this.$refs.saveDialog.open("Edit").setData(row);
            });
        },
        //查看
        opShow(row) {
            this.dialogSave = true;
            this.$nextTick(() => {
                this.$refs.saveDialog.open("Show").setData(row);
            });
        },
        //设置状态
        async setStatus(row) {
            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    row.status == 0 ? "carousel-images/disabled" : "carousel-images/enable",
                    { carouselImagesIds: [row.carouselImagesId] }
                );
                if (returnValue.status == 200) {
                    this.$message.success("操作成功");
                    this.$refs.mytable.getData();
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }
        },
        //删除
        async opDelete(row) {
            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    "carousel-images/delete",
                    { carouselImagesIds: [row.carouselImagesId] }
                );
                if (returnValue.status == 200) {
                    this.$message.success("操作成功");
                    this.$refs.mytable.getData();
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }
        },
        //批量删除
        async opBatchDelete() {
            this.$confirm(
                `确定删除选中的 ${this.selectionRows.length} 项吗？`,
                "提示",
                {
                    type: "warning",
                }
            )
                .then(async () => {
                    const carouselImagesIds = this.selectionRows.map(
                        (p) => p.carouselImagesId
                    );
                    try {
                        var returnValue =
                            await this.$API.auth.PostProcessApi.post(
                                "carousel-images/delete",
                                { carouselImagesIds: [...carouselImagesIds] }
                            );
                        if (returnValue.status == 200) {
                            this.$message.success("操作成功");
                            this.$refs.mytable.getData();
                        } else {
                            this.$message.error("操作失败:" + returnValue.msg);
                        }
                    } catch (error) {
                        this.loading = false;
                        return false;
                    }
                })
                .catch(() => { });
        },
        //表格选择后回调事件
        selectionChange(selection) {
            this.selectionRows = selection;
        },
        //本地更新数据
        handleSaveSuccess(data, mode) {
            if (mode == "Add") {
                this.$refs.mytable.refresh();
            } else if (mode == "Edit") {
                this.$refs.mytable.refresh();
            }
        },
        async opBindData() {
            this.$refs.mytable.clearSort();
            this.$refs.mytable.getData();
        },
        resetSearch() {
            this.queryList = {};
        },
        async initOptions() {
            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    "base-dictionary/options",
                    { Codes: ["Status", "OrgType"] }
                );
                if (returnValue.status == 200) {
                    this.optionsState = returnValue.response.find(
                        (p) => p.code == "Status"
                    ).optionsDetails;
                    this.optionsType = returnValue.response.find(
                        (p) => p.code == "OrgType"
                    ).optionsDetails;
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }
        },
    },
    async mounted() {
        var orgType = tool.data.get("USER_INFO").AccountType;
        await this.initOptions();
        await this.opBindData();
    },
};
</script>
